﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class ReduceConnOperation : ConnOperation
	{
		public ReduceConnOperation(UIDocument uiDoc) : base(uiDoc)
		{
		}

		public override bool SelectDucts()
		{
			bool flag = false;
			if (this.SelectDuctsMethod != null)
			{
				string errorString = "";
				IList<Duct> source = null;
				IList<XYZ> source2 = null;
				flag = this.SelectDuctsMethod.ConnDuctsSelecting(ConnDuctSelectType.ReduceConn, ref source, ref source2, ref errorString);
				if (flag)
				{
					base.SelectedDucts = source.ToList<Duct>();
					base.SelectedPoints = source2.ToList<XYZ>();
				}
				else
				{
					base.ErrorString = errorString;
				}
			}
			return flag;
		}

		public override bool Connect()
		{
			bool flag = false;
			if (base.SelectedDucts.Count < 1)
			{
				base.ErrorString = "选择的风管个数不对！";
				return flag;
			}
			HYDuctConnect hyductConnect = new HYDuctConnect();
			ConnectDuctManage connectDuctManage = new ConnectDuctManage();
			if (!connectDuctManage.BeSameShapeDucts(base.SelectedDucts))
			{
				base.ErrorString = "选择的风管截面必须一致！";
				return flag;
			}
			ConnectDuctStyle connectDuctStyle = base.ConnForm.connStyle;
			int ductShape = connectDuctManage.GetDuctShape(base.SelectedDucts[0]);
			if (ductShape == 1)
			{
				connectDuctStyle = ConnectDuctStyle.Variable;
			}
			else if (ductShape == 0)
			{
				connectDuctStyle = ConnectDuctStyle.VariableRound;
			}
			string empty = string.Empty;
			FamilySymbol familySymbol;
			if (base.ConnForm.Flag)
			{
                familySymbol = (base.UIDoc.Document.GetElementById(base.SelectedDucts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)4, 0).MEPPartId) as FamilySymbol);
				if (familySymbol == null)
				{
					base.ErrorString = "没有可以使用的族配置，请检查Revit的族配置";
					return flag;
				}
			}
			else
			{
				if (connectDuctStyle == ConnectDuctStyle.Variable && ductShape != 1)
				{
					base.ErrorString = "变径截面与风管截面不匹配！";
					return flag;
				}
				if (connectDuctStyle == ConnectDuctStyle.VariableRound && ductShape != 0)
				{
					base.ErrorString = "变径截面与风管截面不匹配！";
					return flag;
				}
				connectDuctManage = new ConnectDuctManage();
				List<string> fittingNames = connectDuctManage.GetFittingNames(connectDuctStyle);
				if (fittingNames.Count != 1)
				{
					base.ErrorString = "获取变径族名称失败！";
					return flag;
				}
				familySymbol = connectDuctManage.GetFamilySymbol(fittingNames[0], base.UIDoc.Document, ref empty);
			}
			FamilyInstance familyInstance = hyductConnect.NewTransitionFitting(base.SelectedDucts, true, familySymbol, ref empty);
			if (familyInstance != null && familySymbol != null)
			{
				flag = true;
				familyInstance.Symbol = familySymbol;
				base.UIDoc.Document.Regenerate();
			}
			if (familySymbol == null)
			{
				base.ErrorString = empty;
				base.UIDoc.Document.Regenerate();
			}
			if (!flag)
			{
				base.ErrorString = empty;
			}
			return flag;
		}

		public override void GetRuleList()
		{
			this.RuleList = UnifiedModified.GetSegmentRuleFitting(base.SelectedDucts.First<Duct>().DuctType, 4);
		}

		public override void SetRuleList()
		{
			if (this.RuleList != null)
			{
				UnifiedModified.SetSegmentRuleFitting(base.SelectedDucts.First<Duct>().DuctType, 4, this.RuleList);
			}
		}
	}
}
